# -*- coding: utf-8 -*-
import json
from elasticsearch import Elasticsearch
from tag.data.es_objects import MomentTag

def create_index():
    create_moment_tag_index = {
        'settings': {
            # just one shard, no replicas for testing
            'number_of_shards': 1,
            'number_of_replicas': 0,
        },
        "mappings": {
            "moment_tag": {
                "properties": {
                    "heat": {
                        "type": "long"
                    },
                    "page_uri": {
                        "type": "string"
                    },
                    "color": {
                        "type": "string"
                    },
                    "return_uri": {
                        "type": "string"
                    },
                    "tag_name": {
                        "properties": {
                            "zh-TW": {
                                "type": "string"
                            },
                            "en": {
                                "type": "string"
                            },
                            "zh-CN": {
                                "type": "string"
                            }
                        }
                    },
                    "tag_type": {
                        "type": "long"
                    },
                    "active": {
                        "type": "boolean"
                    },
                    "created_at": {
                        "format": "dateOptionalTime",
                        "type": "date"
                    },
                    "weight": {
                        "type": "long"
                    },
                    "tag_icon": {
                        "type": "string"
                    },
                    "content": {
                        "analyzer": "ik",
                        "type": "string",
                        "fields": {
                            "tokenized": {
                                "analyzer": "simple",
                                "type": "string"
                            }
                        }
                    },
                    "relation": {
                        "analyzer": "keyword",
                        "type": "string"
                    },
                    "special": {
                        "type": "boolean"
                    },
                    "tag_name_string": {
                        "type": "string"
                    },
                    "tag_max_range": {
                        "type": "long"
                    },
                    "id": {
                        "type": "long"
                    },
                    "tag_min_range": {
                        "type": "long"
                    }
                }
            }
        },
    }

    create_tagging_record_index = {
        'settings': {
            # just one shard, no replicas for testing
            'number_of_shards': 1,
            'number_of_replicas': 0,
        },
        "mappings": {
            "tagging_record": {
                "properties": {
                    "content": {
                        "index": "no",
                        "type": "string",
                        "fields": {
                            "gender": {
                                "analyzer": "simple",
                                "type": "string"
                            },
                            "tag_name": {
                                "analyzer": "keyword",
                                "type": "string"
                            },
                            "moment_tag_id": {
                                "analyzer": "ik",
                                "type": "string"
                            },
                            "total_days": {
                                "analyzer": "simple",
                                "type": "string"
                            }
                        }
                    },
                    "relation": {
                        "analyzer": "keyword",
                        "type": "string"
                    }
                }
            }
        },
    }

    pass

input = """
{"id":2795,
"tag_name":{"zh-CN":"两岁","zh-TW":"两岁"},
"tag_category_id":null,
"weight":100,
"heat":1,
"tag_type":2,
"tag_icon":"http://timehut.qiniucdn.com/cn/tags/orange_20170905.png",
"color":"#ffb62d",
"tag_description":null,
"tag_field_info":null,
"tag_max_range":99999,
"tag_min_range":0,
"active":true,
"special":false,
"return_uri":null,
"created_at":"2017-09-25T04:02:04.734Z",
"updated_at":"2017-09-25T04:21:33.702Z",
"page_uri":null,
"tag_lang":0}
"""
es = Elasticsearch()

def insert_document(index_name, type, _id, _body):
    res = es.index(index=index_name, doc_type=type, id=_id, body=_body)
    # print(res['created'])
    # res = es.get(index=index_name, doc_type=type, id=_id)
    # print(res['_source'])
    es.indices.refresh(index=index_name)

def delete_by_id(index_name, type, _id):
    es.delete(index=index_name, doc_type=type, id=_id)

def delete_by_query(index_name, type, _query):
    es.delete_by_query(index=index_name, doc_type=type, body=_query)

def delete_all(index_name, type):
    _query = {
        "query": {
            "match_all": {}
        }
    }
    es.delete_by_query(index=index_name, doc_type=type, body=_query)

def query(index_name, type, _query):
   res = es.search(index=index_name, doc_type=type, body=_query)
   print(res)

# obj = MomentTag(json.loads(input))
# insert_document(obj.index_name, obj.index_type, obj.id, obj.data)
# delete_by_id(obj.index_name, obj.index_type, obj.id)
# delete_by_query(obj.index_name, obj.index_type, query)

_query = """
{"query":{"bool":{"must":[{"range":{"total_days":{"lte":"10","gte":"0"}}},{"match_phrase":{"active":true}}]}},"aggs":{"group_by_moment_tag_id":{"terms":{"field":"moment_tag_id","size":100},"aggs":{"baby_count":{"cardinality":{"field":"baby_id"}}}}},"size":0}
"""
# query("tagging_record_development", None, _query)

delete_all('tagging_record_development', 'moment_tag')